<?php  
## include required files
/*******************************/
require_once '../model/testimonials.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/

## Create Objects
/*******************************/
$testimonialObj = new Model_Testimonials();
/*******************************/

## Active, Inactive selected records
/********************************/
if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
		$ids = implode(",", $_POST['checkall']);
		## check which action is selected
		$hidden_page = $_POST['hidden_pageno'];	
		if($_POST['action'] == 'active') {
			## Active selected records
			$testimonialObj->updateTestimonialStatus($ids, '1');
			$_SESSION['msg']="<div class='success_msg'><span>Testimonial status activated successfully</span></div>";
			header('location:'.SITE_URL.'/admin/home.php?q=testimonials&page='.$hidden_page);
			exit;
		} else if($_POST['action'] == 'inactive') {
			## Inactive selected records
			$testimonialObj->updateTestimonialStatus($ids, '0');
			$_SESSION['msg']="<div class='success_msg'><span>Testimonial status deactivated successfully</span></div>";
			header('location:'.SITE_URL.'/admin/home.php?q=testimonials&page='.$hidden_page);
			exit;
		} else {
					## Delete selected records
					$testimonialObj->deleteTestimonial($ids);
					$_SESSION['msg']="<div class='success_msg'><span>Testimonial deleted successfully</span></div>";
					if($_GET["numRecords"] > 1)
					{
						header('location:'.SITE_URL.'/admin/home.php?q=testimonials&page='.$_GET['page']);
					}
					else
					{
						$page = $_GET['page'] - 1;
						header('location:'.SITE_URL.'/admin/home.php?q=testimonials&page='.$page);		
					}
					exit;
		}
		
		## Delete code will be here if required
		
} // if close

## Active/Inactive/Delete thourgh URL
if(isset($_GET['action']) == 'status' && isset($_GET['status']) != '') {
	$id = base64_decode($_GET['id']);
	## Update testimonial page status 
	$array = array();
	$array['status'] = ($_GET['status']=='1'?0:1);
	$testimonialObj->editTestimonialById($array, $id);
	$_SESSION['msg']="<div class='success_msg'><span>Testimonial status updated successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=testimonials&page='.$_GET['page']);
	exit;
}

if(isset($_GET['action']) == 'delete' && isset($_GET['id']) != '') {
	$id = base64_decode($_GET['id']);
	## Delete Testimonial 
	$testimonialObj->deleteTestimonialById($id);
	$_SESSION['msg']="<div class='success_msg'><span>Testimonial deleted successfully</span></div>";
	if($_GET["numRecords"] > 1)
	{
		header('location:'.SITE_URL.'/admin/home.php?q=testimonials&page='.$_GET['page']);
	}
	else
	{
		$page = $_GET['page'] - 1;
		header('location:'.SITE_URL.'/admin/home.php?q=testimonials&page='.$page);		
	}
	
	exit;
}

/*******************************/

##Fetch all testimonial pages from database
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Search') {
	$searchindex = addslashes($_POST['search']);
} else if($_GET['search'] != '') {
	$searchindex = addslashes($_GET['search']);
} else {
	$searchindex = '';
}
$smarty->assign('search', stripslashes (stripslashes ($searchindex)));


##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != ""  && $_GET[ 'page' ] > 1) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;
	$pageName = "home.php?q=testimonials"; 
	
	## Count all the records
	$testimonialArray = $testimonialObj->getAllTestimonials($searchindex);
	$total_rows = count($testimonialArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##

##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$testimonialArray = $testimonialObj->getAllTestimonials($searchindex,$rowsPerPage,$offset);
	$smarty->assign('testimonialArray', $testimonialArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}
			
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'testimonials');
$smarty->assign('mainmenu', '2');

## Unset all the objects created which are on this page
unset($testimonialObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'testimonials/testimonial.tpl');
unset($smarty);
?>